package com.lanou.zakerproject.subscribe;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lanou3g.greendaodemo.entity.greendao.NewsScrollEntity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * Created by dllo on 16/1/10.
 */
public class SubscribeScrollAdapter extends PagerAdapter {
    private List<NewsScroll.DataEntity.ListEntity> res;
    private Context context;
    private ViewPager viewPager;
    DisplayImageOptions options;
    List<NewsScrollEntity> newsScrollEntitylist;
    int hasNet = 1;

    public SubscribeScrollAdapter(Context context, List<NewsScroll.DataEntity.ListEntity> res, int hasNet) {
        this.res = res;
        this.context = context;
        this.hasNet = hasNet;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
                .bitmapConfig(Bitmap.Config.ARGB_8888) // default
                .build();
    }

    public SubscribeScrollAdapter(List<NewsScrollEntity> newsScrollEntitylist, Context context, int hasNet) {
        this.newsScrollEntitylist = newsScrollEntitylist;
        this.context = context;
        this.hasNet = hasNet;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
//        if (hasNet == 1) {
//            return res.size();
//        } else {
//            return newsScrollEntitylist.size();
//        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        String entity = res.get(position % res.size());
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if (hasNet == 1) {
            ImageLoader.getInstance().displayImage(res.get(position).getPromotion_img(), imageView, options);
        } else {
            ImageLoader.getInstance().displayImage(newsScrollEntitylist.get(position).getPromotion_img(),imageView,options);
        }
        container.addView(imageView);
        return imageView;
    }


}
